package cn.dodgeball.java;

import cn.dodgeball.util.Const;
import cn.dodgeball.util.PClass;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

public class SquarePanel extends JPanel {
    private Square square = new Square();
    private Timer timer = new Timer();

    public SquarePanel() {
        this.init();
    }

    public void init() {
        setSize(Const.FRAME_WIDTH, Const.FRAME_HEIGHT);
        setOpaque(false);
        setVisible(false);
        setLayout(null);

        // 添加鼠标移动监听器获取鼠标坐标
        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (Const.isGaming)
                    PClass.mousePosition = e.getPoint();
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                if (Const.isGaming)
                    PClass.mousePosition = e.getPoint();
            }
        });
    }

    public void start() {
        setVisible(true);
        square = new Square();
        Thread thread1 = new Thread(square);
        thread1.start();
        add(square);
        timer=new Timer();
       Thread thread2 = new Thread(timer);
        thread2.start();
        add(timer);
        setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), new Point(0, 0), "empty cursor"));
    }

    public void close() {
        setVisible(false);
        remove(square);
        remove(timer);
    }

}

